package com.pig4cloud.pig.module.controller.material;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.Material;
import com.pig4cloud.pig.module.service.base.IMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 素材库Controller
 *
 * @author ruoyi
 * @date 2021-08-13
 */
@Validated
@Api(value = "素材库控制器", tags = {"素材库管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/material")
public class MaterialController {

    private final IMaterialService materialService;

    @Value("${ruoyi.imgUrl}")
	private String imgUrl;

//	@Value("${ruoyi.imgUrlOss}")
//	private String imgUrlOss;

    /**
     * 查询素材库列表
     */
    @ApiOperation("查询素材库列表")
    @GetMapping("/list")
    public TableDataInfo<Material> list(@Validated Material material) {
        QueryWrapper<Material> qw = new QueryWrapper<>();
        if(material.getGroupId()!=null){
            qw.eq("group_id",material.getGroupId());
        }
        qw.orderByDesc("create_time");
		TableDataInfo<Material> td = PageUtils.buildDataInfo(materialService.page(PageUtils.buildPage(),qw));
		List<Material> list = td.getData();
		if(CollUtil.isNotEmpty(list)){
			list.forEach(material1 -> {
				if(StrUtil.isNotEmpty(material1.getUrl()) && (!material1.getUrl().contains("https") && !material1.getUrl().contains("http"))) {
					material1.setImgUrl(imgUrl + material1.getUrl());
				}
			});
		}
        return td;
    }

    /**
     * 获取素材库详细信息
     */
    @ApiOperation("获取素材库详细信息")
    @GetMapping("/{id}")
    public R<Material> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") String id) {
        return R.ok(materialService.getById(id));
    }

    /**
     * 新增素材库
     */
    @ApiOperation("新增素材库")
    @SysLog("素材库")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody Material material) {
//		material.setCreateId(getUser().getNickName());
		material.setCreateTime(new Date());
//		material.setId(UUID.randomUUID().toString());

		if(StrUtil.isNotEmpty(material.getUrl())){
			String[] imgs = material.getUrl().split("\\.");
			material.setFileType(imgs[1].toUpperCase());
		}
        return R.okByRow(materialService.save(material) ? 1 : 0);
    }

    /**
     * 修改素材库
     */
    @ApiOperation("修改素材库")
    @SysLog("素材库")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody Material material) {
		if(StrUtil.isNotEmpty(material.getUrl())){
			String[] imgs = material.getUrl().split("\\.");
			material.setFileType(imgs[1].toUpperCase());
		}
        return R.okByRow(materialService.updateById(material) ? 1 : 0);
    }

    /**
     * 删除素材库
     */
    @ApiOperation("删除素材库")
    @SysLog("素材库")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return R.okByRow(materialService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
